package com.example.wechatdaka.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wechatdaka.entity.User;
import com.example.wechatdaka.service.UserService;
import com.example.wechatdaka.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lx
 * @since 2020-11-05
 */
@Api(tags = "登录模块")
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录账号")
    @RequestMapping(value = "/doLogin",method = RequestMethod.POST)
    @ResponseBody
    public Result login(@RequestBody Map<String,Object> param){
        String username = (String) param.get("username");
        String password = (String) param.get("password");
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("lxUserNumber",username);
        User user = userService.getOne(userQueryWrapper);
        if (user!=null){
            System.out.println(user.toString());
            if (password.equals(user.getLxUserPassword())&&"3".equals(user.getLxRoleId())){
                return new Result(Result.ERROR,null,"学生暂不可登录该系统");
            }else if(password.equals(user.getLxUserPassword())&&!"3".equals(user.getLxRoleId())){
                return new Result(Result.SUCCESS,user.getId(),"登陆成功");
            }
            else {
                return new Result(Result.ERROR,null,"账户密码错误，请重新输入");
            }
        }

        return new Result(Result.ERROR,null,"登录失败（原因：无此用户）");
    }

}
